<?php
declare (strict_types = 1);

namespace app\adminapi\model\organize;

use adminmatrix\admin\model\BaseModel;
use think\Model;
use think\model\relation\HasMany;

/**
 * @mixin \think\Model
 */
class AdminDepartment extends BaseModel
{
    #------------------------------------------------------------
    #
    #   模型事件
    #
    #------------------------------------------------------------

    # 删除后事件
    public static function onAfterDelete(Model $model)
    {
        # 删除职务
        $model->position()->delete();
    }

    #------------------------------------------------------------
    #
    #   关联模型
    #
    #------------------------------------------------------------

    # 关联职务
    public function position() : HasMany
    {
        return $this->hasMany(AdminPosition::class , 'department_id');
    }

    #------------------------------------------------------------
    #
    #   搜索器
    #
    #------------------------------------------------------------

    # 搜索组织id
    public function searchOrganizationIdAttr($query , $organization_id)
    {
        if (empty($organization_id)) return '';
        $query->whereIn('organization_id' , $organization_id);
    }
}
